﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SnippetBox.Core.Common.Caching;
using SnippetBox.Core.Common.Extensions;
using SnippetBox.Core.Data.Models;
using SnippetBox.Core.Data.Service;
using SnippetBox.Web.Areas.Manage.Models;
using SnippetBox.Web.Models;

namespace SnippetBox.Web.Areas.Manage.Controllers
{
    public class CategoriesController : Controller
    {
        CategoryService _categoryService = new CategoryService();
        CacheService _cache = new CacheService();
 
        public ActionResult Index(int? page, string query)
        {
            if (query.IsNullOrEmpty())
                ViewData.Model = _categoryService.GetAllMainPaged(page);
            else
                ViewData.Model = _categoryService.Search(query, page);
            
            ViewBag.IsSearching = query.IsNullOrEmpty();
            
            return View();
        }

        public ActionResult Create() {
            return View(new CategoryModel());
        }

        [HttpPost]
        public ActionResult Create(CategoryModel categoryModel) {
            if (!ModelState.IsValid)
                return View(categoryModel);

            var category = new Category();
            category.Name = categoryModel.Category.Name;
            category.Slug = categoryModel.Category.Slug ?? category.Name.Slugify();
            category.ParentCategory = categoryModel.ParentCategoryId.HasValue
                 ? _categoryService.GetById(categoryModel.ParentCategoryId.Value)
                 : null;

            _categoryService.Create(category);
            _categoryService.Save();
            
            _cache.RemoveAll("categoryMenu");
            
            return RedirectToAction("Index");
        }

        public ActionResult Edit(int id) {
            var category = _categoryService.GetById(id);

            if (category == null)
                return View("NotFound", new NotFoundModel());

            var model = new CategoryModel(category);

            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(int id, CategoryModel categoryModel) {
            if (!ModelState.IsValid)
                return View(categoryModel);

            var category = _categoryService.GetById(id);
            category.Name = categoryModel.Category.Name;
            category.Slug = categoryModel.Category.Slug ?? category.Name.Slugify();
            
            category.ParentCategory = categoryModel.ParentCategoryId.HasValue 
                ? _categoryService.GetById(categoryModel.ParentCategoryId.Value) 
                : null;

            _categoryService.Save();
            _cache.RemoveAll("categoryMenu");

            return RedirectToAction("Index");
        }

        public ActionResult Delete(int id) {
            var user = _categoryService.GetById(id);

            if (user == null)
                return View("NotFound", new NotFoundModel());

            return View(user);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeletePost(int id) {
            var category = _categoryService.GetById(id);

            if (category == null)
                return View("NotFound", new NotFoundModel());

            // TODO: Delete sub categories?

            category.IsDeleted = !category.IsDeleted;
            _categoryService.Save();

            _cache.RemoveAll("categoryMenu");

            if (Request.IsAjaxRequest())
                return Json(new { subject = category.Name, predicate = " was deleted" });

            return RedirectToAction("Index");
        }

    }
}
